package com.frkj.order.controller;

import com.frkj.order.cache.LoginOperatorCache;
import com.frkj.order.model.LoginOperator;
import com.frkj.order.model.Operator;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/3.
 */
public class LoginOperatorController extends Controller {

    public void index() {
        List<LoginOperator> loginOperatorList = LoginOperator.me.find("select * from loginoperator order by window asc");

        // 管理员权限列表
        List<String> operatorBusinessList = new ArrayList<String>();
        for(LoginOperator loginOperator : loginOperatorList) {
            List<Record> recordList = Db.find("select name from business where id in (select business_id from operator_business where operator_id=?)", loginOperator.getInt("operator_id"));
            String[] businessNames = new String[recordList.size()];
            for(int i = 0; i < recordList.size(); i++) {
                businessNames[i] = recordList.get(i).getStr("name");
            }
            operatorBusinessList.add(Arrays.toString(businessNames));
        }

        setAttr("loginOperatorList", loginOperatorList);
        setAttr("operatorBusinessList", operatorBusinessList);

        render("index.html");
    }

    public void save() {
        render("edit.html");
    }

    public void login() {
        // 检查窗口是否已有业务员登陆
        int window = getParaToInt("window", -1);
        LoginOperator checkObj = LoginOperator.me.findById(window);
        if(checkObj != null) {
            setAttr("showMessage", "窗口" + window + "已有办事员登陆，请确认登陆窗口或退出已登陆办事员");
        } else {
            String name = getPara("name");
            String password = getPara("password");
            String enPasswrd = DigestUtils.sha1Hex(password);
            Operator operator = Operator.me.findFirst("select * from operator where name=? and password=?", name, enPasswrd);
            if (operator == null) {
                setAttr("showMessage", "登录失败，请重新检查用户名及口令");
            } else {
                setAttr("showMessage", "登录成功");
                LoginOperator loginOperator = new LoginOperator();
                loginOperator.set("operator_id", operator.getInt("id"));
                loginOperator.set("operator_name", operator.getStr("name"));
                loginOperator.set("login_time", new Date());
                loginOperator.set("window", window);
                loginOperator.save();

                LoginOperatorCache.loadData();
            }
        }
        index();
    }

    public void logout() {
        int window = getParaToInt("window", -1);
        LoginOperator.me.deleteById(window);

        LoginOperatorCache.loadData();

        setAttr("showMessage", "登出操作成功");
        index();
    }

    public void logoutAll() {
        Db.find("delete from loginoperator");

        LoginOperatorCache.loadData();

        setAttr("showMessage", "全部登出成功");
        index();
    }
}
